function Ptf = CriterionRebalancing(Data,Ptf,Sectors)
        
    Quantile = Ptf.Quantiles;

    Intensity=Data.Intensity12;
    Scope=Data.Scope12;
    
    Intensity(isnan(Intensity)) = 0;

    Weight=Data.WeightsRescaled;
    tmp=Scope./Data.MktCap; 
    idx = ~isnan(tmp);
    Weight(isnan(tmp))=0;
    Weight(idx)=Weight(idx)/sum(Weight(idx));

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    if Ptf.screen == 0
    
        tmp = [Intensity Weight];
        tmp2 = sortrows(tmp,1,'descend','MissingPlacement','last');
        tmp2(:,2) = cumsum(tmp2(:,2));
        indx = find(tmp2(:,2)<=Quantile);
        if isempty(indx)
            indx=1;
        end
        ThresholdU = tmp2(indx(end),1);

        tmp2 = sortrows(tmp,1,'ascend','MissingPlacement','last');           
        tmp2(:,2) = cumsum(tmp2(:,2));
        indx = find(tmp2(:,2)<=Quantile);
        if isempty(indx)
            indx=1;
        end
        ThresholdD = tmp2(indx(end),1);

        CriterionExcl = logical(Intensity>=ThresholdU);
        CriterionIncl = logical(Intensity<=ThresholdD);

        WeightExcl = Weight(CriterionExcl);
        WeightIncl = Weight(CriterionIncl);
        sumWeightExcl = sum(WeightExcl);
        sumWeightIncl = sum(WeightIncl);

        pWeights = Weight;
        pWeights(CriterionIncl) = WeightIncl * (1+sumWeightExcl/sumWeightIncl);

        pWeights = pWeights(~CriterionExcl);
        
        Data.WeightsAdded(CriterionIncl) = WeightIncl * (sumWeightExcl/sumWeightIncl);
        
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    elseif Ptf.screen == 1   % by sector
        
        Sectors(strcmp(Sectors,'-')) = [];
        GICSS = Data.Sector;
        nS = size(Sectors,1);
        
        CriterionExcl = zeros(length(Intensity),1);
        CriterionIncl = zeros(length(Intensity),1);
        pWeights = Weight;
        WeightIncluded = zeros(nS,1);

        for i=1:nS
            
           idx= (categorical(GICSS)==Sectors{i,1});

            MktCap = Weight(idx)/sum(Weight(idx));
            tmp = [Intensity(idx) MktCap];
            tmp2 = sortrows(tmp,1,'descend','MissingPlacement','last');    
            tmp2(:,2) = cumsum(tmp2(:,2));
            indx = find(tmp2(:,2)<=Quantile);
            if isempty(indx)
                indx=1;
            end
            ThresholdU = tmp2(indx(end),1);

            tmp2 = sortrows(tmp,1,'ascend','MissingPlacement','last');    
            tmp2(:,2) = cumsum(tmp2(:,2));
            indx = find(tmp2(:,2)<=Quantile);
            if isempty(indx)
                indx=1;
            end
            ThresholdD = tmp2(indx(end),1);
          
            CriterionExcl(idx) = logical(Intensity(idx)>=ThresholdU);
            CriterionIncl(idx) = logical(Intensity(idx)<=ThresholdD);
                  
            CriterionIncl2 = zeros(length(Intensity),1);
      	    CriterionIncl2(idx) = logical(Intensity(idx)<=ThresholdD);
            idx2 = CriterionIncl2.*(Intensity<=ThresholdD);

            Weightsi = Weight(idx);
            WeightExcl = Weightsi(logical(CriterionExcl(idx)));
            WeightIncl = Weightsi(logical(CriterionIncl(idx)));
            sumWeightExcl = sum(WeightExcl);
            sumWeightIncl = sum(WeightIncl);
            WeightIncluded(i) = sumWeightIncl;

            Weightsi(logical(CriterionExcl(idx))) = 0;
            Weightsi(logical(CriterionIncl(idx))) = WeightIncl * (1+sumWeightExcl/sumWeightIncl);

            pWeights(idx) = Weightsi;
            
            Data.WeightsAdded(logical(idx2)) = WeightIncl * (sumWeightExcl/sumWeightIncl);          
            
        end
        
        pWeights = pWeights(~CriterionExcl);
        
    end
    
    CriterionExcl = logical(CriterionExcl);
    CriterionIncl = logical(CriterionIncl);
            
    WeightExcl = Weight(CriterionExcl);
    WeightIncl = Data.WeightsAdded;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    Ptf.NbFirmsExcluded = sum(CriterionExcl);
    Ptf.MktCapExcluded = sum(WeightExcl);
    Ptf.NbFirmsIncluded = sum(CriterionIncl);
    Ptf.MktCapIncluded = sum(WeightIncl);
        
    keepScore = ~isnan(Scope);
    CleanWeights = Weight(keepScore);
    tmp=(Scope(keepScore))./Data.Revenues(keepScore);
    idx = ~isnan(tmp);
    InitWAIntensity = sum(CleanWeights(idx) .* tmp(idx)) / sum(CleanWeights(idx));
    disp('Initial WAIntensity')
    disp(InitWAIntensity)

    keepScore = ~isnan(Scope);
    CleanWeights = Weight(keepScore);
    tmp=(Scope(keepScore))./Data.MktCap(keepScore);
    idx = ~isnan(tmp);
    InitFootprint = sum(CleanWeights(idx) .* tmp(idx))*1000000 / sum(CleanWeights(idx));
    disp('Initial Footprint')
    disp(InitFootprint)

    disp('List of firms with highest carbon footprint')
    Texcl = table(Data.Name(CriterionExcl,:),Intensity(CriterionExcl,:),Weight(CriterionExcl,:),Scope(CriterionExcl,:),Data.MktCap(CriterionExcl,:),Data.x_OfCSO(CriterionExcl,:),Data.MarketValue(CriterionExcl,:),Data.Revenues(CriterionExcl,:),Data.Sector(CriterionExcl,:),'VariableNames',{'Name','Intensity','Weight','Scope','MktCap','CSO','MarketValue','Revenues','Sector'});
    Texcls = sortrows(Texcl,2,'descend');
    tmp=Texcl.Scope./Texcl.Revenues;
    idx = ~isnan(tmp);
    ExcludedWAIntensity = sum(Texcl.Weight(idx) .* tmp(idx)) / sum(Texcl.Weight(idx));
    tmp=Texcl.Scope./Texcl.MktCap;
    idx = ~isnan(tmp);
    ExcludedFootprint = sum(Texcl.Weight(idx) .* tmp(idx)) / sum(Texcl.Weight(idx))*1000000;
    cumulex = table(cumsum(Texcls.Weight),'VariableNames',{'Cumul'});
    disp([Texcls cumulex])
    disp('Number of firms excluded')
    disp(size(Texcls,1))
    disp('Exclusion threshold')
    disp(ThresholdU)
    disp('Fraction of total equity excluded')
    disp(cumulex{end,:})
    disp('Excluded WAIntensity')
    disp(ExcludedWAIntensity*cumulex{end,:})
    disp('Excluded Footprint')
    disp(ExcludedFootprint*cumulex{end,:})

    disp('List of firms with lowest carbon footprint')
    Tincl = table(Data.Name(CriterionIncl,:),Intensity(CriterionIncl,:),Data.WeightsAdded(CriterionIncl,:),Scope(CriterionIncl,:),Data.MktCap(CriterionIncl,:),Data.x_OfCSO(CriterionIncl,:),Data.MarketValue(CriterionIncl,:),Data.Revenues(CriterionIncl,:),Data.Sector(CriterionIncl,:),'VariableNames',{'Name','Intensity','Weight','Scope','MktCap','CSO','MarketValue','Revenues','Sector'});
    Tincls = sortrows(Tincl,2,'ascend');
    tmp=Tincl.Scope./Tincl.Revenues;
    idx = ~isnan(tmp);
    AddedWAIntensity = sum(Tincl.Weight(idx) .* tmp(idx)) / sum(Tincl.Weight(idx));
    tmp=Tincl.Scope./Tincl.MktCap;
    idx = ~isnan(tmp);
    AddedFootprint = sum(Tincl.Weight(idx) .* tmp(idx)) / sum(Tincl.Weight(idx))*1000000;
    cumulin = table(cumsum(Tincls.Weight),'VariableNames',{'Cumul'});
    disp([Tincls cumulin])
    disp('Number of firms included')
    disp(size(Tincls,1))
    disp('Overweighting threshold')
    disp(ThresholdD)
    disp('Fraction of total equity included')
    disp(cumulin{end,:})
    disp('Added WAIntensity')
    disp(AddedWAIntensity*cumulin{end,:})
    disp('Added Footprint')
    disp(AddedFootprint*cumulin{end,:})

  
    DataAfterExclusion = Data;
    DataAfterExclusion(CriterionExcl,:) = [];
    NewWeights = pWeights / sum(pWeights);
    
    keepScore = ~isnan(DataAfterExclusion.Scope12);
    CleanWeights = NewWeights(keepScore) / sum(NewWeights(keepScore));
    tmp=(DataAfterExclusion.Scope12(keepScore))./DataAfterExclusion.Revenues(keepScore);
    idx = ~isnan(tmp);
    Ptf.WAIntensity = sum(CleanWeights(idx) .* tmp(idx)) / sum(CleanWeights(idx));
    disp('Final WAIntensity')
    disp(Ptf.WAIntensity)

    tmp=(DataAfterExclusion.Scope12(keepScore))./DataAfterExclusion.MktCap(keepScore);
    idx = ~isnan(tmp);
    Ptf.Footprint = sum(CleanWeights(idx) .* tmp(idx)) / sum(CleanWeights(idx))*1000000;
    disp('Final Footprint')
    disp(Ptf.Footprint)
    
    tmp1=DataAfterExclusion.Scope12(keepScore) .* DataAfterExclusion.x_OfCSO(keepScore);
    tmp2=DataAfterExclusion.Revenues(keepScore) .* DataAfterExclusion.x_OfCSO(keepScore);
    idx = ~isnan(tmp1.*tmp2);
    Ptf.Intensity = sum(tmp1(idx)) / sum(tmp2(idx));

    tmp=DataAfterExclusion.TRIp1(keepScore)./DataAfterExclusion.TRI(keepScore)-1;
    idx = ~isnan(tmp);
    Ptf.AnnualReturn = sum(CleanWeights(idx) .* tmp(idx)) / sum(CleanWeights(idx));

